package cn.kgc.javaweb.service.impl;

import cn.kgc.javaweb.dao.RepairDao;
import cn.kgc.javaweb.dao.impl.RepairDaoImpl;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.Repair;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.service.RepairService;
import cn.kgc.javaweb.util.ResponseResult;

import java.util.List;

/**
 * @author 涛涛
 * @version 1.0
 */
public class RepairServiceImpl implements RepairService {
    private RepairDao repairDao = new RepairDaoImpl();

    @Override
    public ResponseResult selectPage(UserPageDTO userPageDTO) {
        ResponseResult responseResult = new ResponseResult();
        try {
            List<Repair> repairs = repairDao.selectPage(userPageDTO);

            Long total = repairDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(repairs);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public void state(boolean checked, Integer id) {
        repairDao.state(checked,id);
    }

    @Override
    public ResponseResult update(Repair repair) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            repairDao.update(repair);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult add(Repair repair) {

        ResponseResult responseResult = ResponseResult.builder().build();

            try {
                repairDao.add(repair);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }



        return responseResult;
    }

    @Override
    public ResponseResult delete(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            repairDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }

}